#include "../../stdafx.h"


#ifdef COMMS_WINDOWS

//#include <windows.h>
//#include <stdlib.h>
typedef HANDLE ResFile;
//Opening the resource file
ResFile RReset(LPCSTR lpFileName)
{
	SetLastError(0);
	return CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ,NULL,
		                         OPEN_EXISTING,0,NULL);
}
//Rewriting file
ResFile RRewrite(LPCSTR lpFileName)
{
	SetLastError(0);
	return CreateFile(lpFileName,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
		                         CREATE_ALWAYS,0,NULL);
}
//Getting size of the resource file
DWORD RFileSize(HANDLE hFile)
{
	SetLastError(0);
	return GetFileSize(hFile,NULL);
}
// Setting file position 
DWORD RSeek(ResFile hFile,int pos)
{
	SetLastError(0);
	return SetFilePointer(hFile,pos,NULL,FILE_BEGIN);
}

DWORD RSeekFromCurrent(ResFile hFile,int nBytes)
{
	SetLastError(0);
	return SetFilePointer(hFile,nBytes,NULL,FILE_CURRENT);
}

//Reading the file
DWORD RBlockRead(ResFile hFile,LPVOID lpBuffer,DWORD BytesToRead)
{
	SetLastError(0);
	DWORD readBytes;
	ReadFile(hFile,lpBuffer,BytesToRead,&readBytes,NULL);
	return readBytes;
}
//Writing the file
DWORD RBlockWrite(ResFile hFile,LPVOID lpBuffer,DWORD BytesToWrite)
{
	SetLastError(0);
	DWORD writeBytes;
	WriteFile(hFile,lpBuffer,BytesToWrite,&writeBytes,NULL);
	return writeBytes;
}
DWORD IOresult(void)
{
	return GetLastError();
}
void RClose(ResFile hFile)
{
	SetLastError(0);
	if(!CloseHandle(hFile))
		MessageBox(NULL,"Failed to close file!","Strange error",0);
}

#else // !COMMS_WINDOWS

typedef HANDLE ResFile;
//Opening the resource file
ResFile RReset(LPCSTR lpFileName)
{
	if(lpFileName){
		FILE* F=fopen(lpFileName,"rb");
		if(F)return (ResFile)F;
		else return INVALID_HANDLE_VALUE;	
	}else return INVALID_HANDLE_VALUE;	
}
//Rewriting file
ResFile RRewrite(LPCSTR lpFileName)
{
	if(lpFileName){
		FILE* F=fopen(lpFileName,"wb");
		if(F)return (ResFile)F;
		else return INVALID_HANDLE_VALUE;
	}else return INVALID_HANDLE_VALUE;
}
//Getting size of the resource file
DWORD RFileSize(HANDLE hFile)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		int cp=ftell(F);
		fseek(F,0,SEEK_END);
		int ps=ftell(F);
		fseek(F,cp,SEEK_SET);
		return ps;
	}else return 0;	
}
// Setting file position 
DWORD RSeek(ResFile hFile,int pos)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		int cp=fseek(F,pos,SEEK_SET);		
		return cp;
	}else return 0;	
}

DWORD RSeekFromCurrent(ResFile hFile,int nBytes)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		int cp=fseek(F,nBytes,SEEK_CUR);		
		return cp;
	}else return 0;	
}

//Reading the file
DWORD RBlockRead(ResFile hFile,LPVOID lpBuffer,DWORD BytesToRead)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		return fread(lpBuffer,1,BytesToRead,F);
	}else return 0;
}
//Writing the file
DWORD RBlockWrite(ResFile hFile,LPVOID lpBuffer,DWORD BytesToWrite)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		return fwrite(lpBuffer,1,BytesToWrite,F);
	}else return 0;
}
DWORD IOresult(void)
{
	return 0;
}
void RClose(ResFile hFile)
{
	if(hFile!=INVALID_HANDLE_VALUE){
		FILE* F=(FILE*)hFile;
		fclose(F);
	}
}

#endif //COMMS_WINDOWS